package com.linq.cool.beauty.service;

import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.builder.AlipayTradeRefundRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.model.result.AlipayF2FRefundResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.linq.cool.beauty.dto.AlipayPayDTO;
import com.linq.cool.beauty.dto.AlipayRefundDTO;
import com.linq.cool.beauty.vo.AlipayRefundVO;
import com.linq.cool.beauty.vo.AlipayVO;
import com.linq.cool.common.core.domain.Result;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;

/**
 * @Author: yqlin
 * @Date: 2021/1/20 21:42
 * @Description:
 * @Version: 1.0.0
 */
public class AlipayService {
    private static final Log log = LogFactory.getLog("trade_precreate");
    /**
     * 支付宝当面付对象
     */
    private static final AlipayTradeService tradeService;
    /**
     * 商户操作员编号，添加此参数可以为商户操作员做销售统计
     */
    private static final String operatorId = "test_operator_id";
    /**
     * (必填) 商户门店编号，通过门店号和商家后台可以配置精准到门店的折扣信息，详询支付宝技术支持
     */
    private static final String storeId = "test_store_id";
    /**
     * 支付超时，定义为120分钟
     */
    private static final String timeoutExpress = "120m";

    static {
        Configs.init("ali-pay.properties");
        tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();
    }

    /**
     * 支付的方法
     *
     * @return 结果
     */
    public static Result<?> pay(AlipayPayDTO dto) {
        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
                .setSubject(dto.getSubject())
                .setTotalAmount(dto.getTotalAmount())
                .setOutTradeNo(dto.getOutTradeNo())
                .setUndiscountableAmount(dto.getUndiscountableAmount())
                .setBody(dto.getBody())
                .setOperatorId(operatorId)
                .setStoreId(storeId)
                .setTimeoutExpress(timeoutExpress)
                // 支付宝服务器主动通知商户服务器里指定的页面http路径,根据需要设置
                .setNotifyUrl(dto.getNotifyUrl());
        // .setNotifyUrl("http://www.test-notify-url.com");
        // 发起支付
        AlipayF2FPrecreateResult result = tradeService.tradePrecreate(builder);
        switch (result.getTradeStatus()) {
            case SUCCESS:
                log.info("支付宝预下单成功: )");
                AlipayTradePrecreateResponse res = result.getResponse();
                // 得到支付码,本质就是一个url
                String zfbQrCode = res.getQrCode();
                return Result.ok(new AlipayVO().setQrCode(zfbQrCode), "支付宝预下单成功");
            case FAILED:
                log.error("支付宝预下单失败!!!");
                return Result.fail("支付宝预下单失败");
            case UNKNOWN:
                log.error("系统异常，预下单状态未知!!!");
                return Result.fail(HttpStatus.NOT_FOUND.value(), "系统异常，预下单状态未知");
            default:
                log.error("不支持的交易状态，交易返回异常!!!");
                return Result.fail(HttpStatus.NOT_FOUND.value(), "不支持的交易状态，交易返回异常");
        }
    }


    /**
     * 退费
     *
     * @return 结果
     */
    public static Result<?> payBack(AlipayRefundDTO dto) {
        AlipayTradeRefundRequestBuilder builder = new AlipayTradeRefundRequestBuilder()
                //outTradeNo tradeNo只需要有一个就行
                .setOutTradeNo(dto.getOutTradeNo())
                .setTradeNo(dto.getTradeNo())
                .setRefundAmount(dto.getRefundAmount())
                .setRefundReason(dto.getRefundReason())
                .setOutRequestNo(dto.getOutRequestNo())
                .setStoreId(storeId);
        //退费请求
        AlipayF2FRefundResult result = tradeService.tradeRefund(builder);
        switch (result.getTradeStatus()) {
            case SUCCESS:
                log.info("支付宝退款成功: )");
                return Result.ok(new AlipayRefundVO().setTradeNo(result.getResponse().getTradeNo()), "支付宝退款成功");
            case FAILED:
                log.error("支付宝退款失败!!!");
                return Result.fail(result.getResponse().getSubMsg());
            case UNKNOWN:
                log.error("系统异常，订单退款状态未知!!!");
                return Result.fail(result.getResponse().getSubMsg());
            default:
                log.error("不支持的交易状态，交易返回异常!!!");
                return Result.fail(result.getResponse().getSubMsg());
        }
    }

}
